package com.yanqu.road.server.cmd.rank;

import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.pb.activity.DaoMuProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ChildModule;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.sacrifice.SacrificeMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;

@CmdApi(reqMsg = RankProto.RankListReqMsg.class, respMsg =  RankProto.RankListRespMsg.class)
@Cmd(code = Protocol.S_USER_RANK_LIST, desc = "获取用户排行榜数据")
public class UserRankListCmd extends AbstractCommand {

    private int bigRankType;

    private int rankType;
    private int pageNo;
    private int pageSize;

    private int objectId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.RankListReqMsg reqMsg = RankProto.RankListReqMsg.parseFrom(packet.getBytes());
        rankType = reqMsg.getRankType();
        bigRankType = reqMsg.getBigRankType();
        pageNo = reqMsg.getPageNo();
        pageSize = reqMsg.getPageSize();
        if(reqMsg.hasObjectId()){
            objectId = reqMsg.getObjectId();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        RankProto.RankListRespMsg.Builder respMsg = RankProto.RankListRespMsg.newBuilder();
        respMsg.setBigRankType(bigRankType);
        respMsg.setRankType(rankType);
        RankProto.RankListMsg.Builder rankListMsg;
        if (bigRankType == eBigRankType.Normal.getValue()) {
            if (eRankType.CrossEarnSpeed.getValue() == rankType || eRankType.CrossPatronsAbility.getValue() == rankType ||
                    eRankType.CrossBeautyIntimacy.getValue() == rankType || eRankType.EmbroideryMeter.getValue() == rankType ||
                    eRankType.CrossManorProsperousValue.getValue() == rankType || eRankType.CrossWineShopPopularityValue.getValue() == rankType ||
                    eRankType.CrossFishPondScore.getValue() == rankType || eRankType.CrossGrandChildEarnSpeed.getValue() == rankType ||
                    eRankType.CrossAssistantDressOnValue.getValue() == rankType || eRankType.CrossWingRoomComfortValue.getValue() == rankType ||
                    eRankType.CrossDrugStroeTongBiValue.getValue() == rankType || eRankType.CrossWineryAroma.getValue() == rankType ||
                    eRankType.CrossCricketScore.getValue() == rankType || eRankType.CrossKowloonProsperousValue.getValue() == rankType ||
                    eRankType.CrossWingRoomWindWaterValue.getValue() == rankType ||
                    eRankType.CrossMusicRoomRank.getValue() == rankType ||
                    eRankType.CurioCrossScoreValue.getValue() == rankType
            ) {
                ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
                if (null != serverGroup && serverGroup.isCrossRank()) {
                    int type = RankMgr.parseCrossRankType(rankType);
                    sendCrossReq(player, bigRankType, type, rankType);
                }
                return;
            } else if (eRankType.CorssHistoryMaxAbility.getValue() == rankType) {
                //找商会跨服请求排行
                CrossUnionServerGroup serverGroup = CrossUnionServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
                if (null != serverGroup && serverGroup.getState() == 1) {
                    int type = RankMgr.parseCrossRankType(rankType);
                    sendCrossReqToUnionCross(player, bigRankType, type, rankType, pageNo, pageSize);
                }
                return;
            } else {
                RankListModel rankListModel;
                if (eRankType.SacrificeDamage.getValue() == rankType) {
                    rankListModel = SacrificeMgr.getSacrificeDamageRankList();
                } else {
                    rankListModel = RankMgr.getRankListModelByType(eBigRankType.Normal.getValue(), rankType);
                }

                if (null != rankListModel) {
                    rankListMsg = rankListModel.getRankListMsg();
                    respMsg.setMyRank(rankListModel.unlockGetMyRank(player.getUserId()));
                } else {
                    rankListMsg = RankProto.RankListMsg.newBuilder();
                    respMsg.setMyRank(-1);
                }
            }
            if (eRankType.ChildSpeed.getValue() == rankType) {
                BigInteger earnSpeed = player.getModule(ChildModule.class).getPowerChildEarnSpeed();
                respMsg.setParam(earnSpeed.toString());
            }
            respMsg.setRankList(rankListMsg);
            player.sendPacket(Protocol.U_USER_RANK_LIST, respMsg);
            return;
        } else if(bigRankType == eBigRankType.CurioItem.getValue()) {
            if(rankType == eRankType.CurioCrossScoreValue.getValue()){
                //跨服
                ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
                if (serverGroup != null && serverGroup.isCrossRank()) {
                    sendCrossReq(player, bigRankType, objectId, objectId);
                    return;
                }
            }else {
                RankListModel rankListModel = RankMgr.getRankListModelByType(bigRankType, objectId);
                sendLocalResp(player, respMsg, rankListModel);
                return;
            }
        } else {
            //6.5.0 皮肤榜单，排行ID转换（大陆美女）
            if (bigRankType == eBigRankType.Skin.getValue()) {
                rankType = SkinMgr.rankBeautySkinIdTransform(rankType);
            }

            //7.4.0 【爱徒型门客】榜单的处理（门客榜单）
            if (bigRankType == eBigRankType.Patrons.getValue() && rankType != 0 && PatronsMgr.getPatronsInfo(rankType) != null) {
                rankType = PatronsMgr.getPatronsRankRankType(rankType);
            }
            //7.4.0 【爱徒型门客】榜单的处理（皮肤榜单）
            if (bigRankType == eBigRankType.Skin.getValue()) {
                rankType = PatronsMgr.getPatronsSkinRankRankType(rankType);
            }

            ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
            if (serverGroup != null && serverGroup.isCrossRank()) {
                sendCrossReq(player, bigRankType, rankType, rankType);
                return;
            } else {
                RankListModel rankListModel = RankMgr.getRankListModelByType(bigRankType, rankType);
                sendLocalResp(player, respMsg, rankListModel);
                return;
            }
        }
    }

    private void sendCrossReq(GamePlayer player, int bigRankType, int crossType, int gameType) {
        RankProto.CrossGroupRankReqMsg.Builder req = RankProto.CrossGroupRankReqMsg.newBuilder();
        req.setGroupId(ServerGroupMgr.getGroupIdByServerId(GameServer.getInstance().getServerId()));
        req.setCorssType(crossType);
        req.setGameType(gameType);
        req.setBigRankType(bigRankType);
        player.sendPacket(CrossProtocol.C_CROSS_GROUP_RANK, req);
    }

    private void sendCrossReqToUnionCross(GamePlayer player, int bigRankType, int crossType, int gameType, int pageNo, int pageSize) {
        RankProto.CrossGroupRankReqMsg.Builder req = RankProto.CrossGroupRankReqMsg.newBuilder();
        req.setGroupId(CrossUnionServerGroupMgr.getGroupIdByServerId(GameServer.getInstance().getServerId()));
        req.setCorssType(crossType);
        req.setGameType(gameType);
        req.setBigRankType(bigRankType);
        req.setPageNo(pageNo);
        req.setPageSize(pageSize);
        player.sendPacket(CrossProtocol.C_UNION_CROSS_GROUP_RANK, req);
    }

    private void sendLocalResp(GamePlayer player, RankProto.RankListRespMsg.Builder respMsg, RankListModel rankListModel) {
        RankProto.RankListMsg.Builder rankListMsg;
        if (rankListModel != null) {
            rankListMsg = rankListModel.getRankListMsg();
            respMsg.setMyRank(rankListModel.unlockGetMyRank(player.getUserId()));
        } else {
            rankListMsg = RankProto.RankListMsg.newBuilder();
            respMsg.setMyRank(-1);
        }
        respMsg.setRankList(rankListMsg);
        player.sendPacket(Protocol.U_USER_RANK_LIST, respMsg);
    }
}
